package edu.unr.cater.npap.share.domain;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Query;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class ProjectContacts {

    @ManyToOne
    private Contact contact;

    @ManyToMany(cascade = CascadeType.ALL)
    private Set<ContactRole> roles = new HashSet<ContactRole>();

    @ManyToOne
    private Project idproject;
    
    public static List<ProjectContacts> findAllProjectContactses(Integer idProject) {
    	List<Project> idprojects = Arrays.asList(Project.findProject(idProject));
        Query query=entityManager().createQuery("SELECT o FROM ProjectContacts o WHERE o.idproject in (:idprojects)", ProjectContacts.class);
    	query.setParameter("idprojects", idprojects);
        return query.getResultList();
        
    }
    
    public static List<ProjectContacts> findProjectContactsEntries(int firstResult, int maxResults,Integer idProject) {
    	List<Project> idprojects = Arrays.asList(Project.findProject(idProject));
    	Query query=entityManager().createQuery("SELECT o FROM ProjectContacts o WHERE o.idproject in (:idprojects)", ProjectContacts.class).setFirstResult(firstResult).setMaxResults(maxResults);
    	query.setParameter("idprojects", idprojects);
        return query.getResultList();
    }
    
   
}
